<template>
  <d2-container :filename="filename">
    <el-row>
      <el-form label-width="80px" ref="updateForm" :inline="true">
        <el-form-item>
          <el-button
            type="primary"
            v-has="'search'"
            @click="search()"
            icon="el-icon-search"
            >查询</el-button
          >
          <el-button
            type="primary"
            v-has="'add'"
            @click="add()"
            icon="el-icon-plus"
            >新增</el-button
          >
        </el-form-item>
      </el-form>
      <el-form label-width="80px" ref="searchForm" :inline="true">
        <el-form-item label="关键词" prop="keyword">
          <el-input v-model="roleSearch.keyword" clearable />
        </el-form-item>
      </el-form>
    </el-row>

    <el-row>
      <el-table
        :data="roleTableData"
        stripe
        style="width: 100%"
        element-loading-text="加载中"
        v-loading="tabloading"
        border=""
      >
        <el-table-column type="index"></el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <div>
              <el-tooltip
                class="item"
                effect="dark"
                content="删除"
                placement="top-start"
              >
                <el-button
                  type="danger"
                  v-has="'delete'"
                  size="mini"
                  icon="el-icon-delete"
                  @click="deleteRow(scope.row)"
                  circle
                ></el-button>
              </el-tooltip>
              <el-tooltip
                class="item"
                effect="dark"
                content="修改"
                placement="top-start"
              >
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  @click="updateRow(scope.row)"
                  size="mini"
                  v-has="'edit'"
                  circle
                ></el-button>
              </el-tooltip>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="displayName" label="角色名称"> </el-table-column>
        <el-table-column prop="name" label="角色代码"> </el-table-column>
        <!-- <el-table-column prop="isStatic" label="是否锁定">
            <template slot-scope="scope">
              <el-checkbox v-model="scope.row.isStatic" disabled></el-checkbox>
            </template>
          </el-table-column> -->
      </el-table>
      <el-row>
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[20, 50, 100, 500]"
          :page-size="roleSearch.maxResultCount"
          layout="total, sizes, prev, pager, next, jumper"
          :total="roleSearch.total"
        >
        </el-pagination>
      </el-row>
    </el-row>
    <createModel ref="createModel" @fromvisible="pageReload"></createModel>
  </d2-container>
</template>

<script>
import { GetRoleListAll, Delete } from '@api/systemManage/roleManage'
import createModel from '@/pages/systemManage/roleManage/createModel'
export default {
  name: 'roleManage',
  inject: ['reload'],
  components: { createModel },
  data () {
    return {
      filename: __filename,
      roleSearch: {
        skipCount: 0,
        maxResultCount: 20,
        total: 0,
        keyword: ''

      },
      roleTableData: [],
      tabloading: false
    }
  },
  created () {
    this.search()
  },
  methods: {
    search () {
      this.roleSearch.skipCount = 0
      this.roleSearch.maxResultCount = 20
      this.getRoleList()
    },
    getRoleList () {
      this.tabloading = true
      GetRoleListAll(this.roleSearch).then(res => {
        this.tabloading = false
        if (res.success) {
          this.roleTableData = res.result.items
          this.roleSearch.total = res.result.totalCount
        }
      })
    },
    deleteRow (row) {
      if (row.isStatic) {
        this.$message({
          message: '[' + row.name + ']已锁定,不能删除',
          type: 'warning'
        })
        return
      }
      this.$confirm('是否删除角色[' + row.name + ']?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        Delete(row.id).then(res => {
          if (res.success) {
            this.$notify({
              title: '提示',
              message: '删除成功',
              type: 'success'

            })
            this.search()
          }
        })
      })
    },
    pageReload () {
      this.search()
    },
    handleSizeChange (val) {
      this.roleSearch.maxResultCount = val
      this.getRoleList()
    },
    handleCurrentChange (val) {
      this.roleSearch.skipCount = val - 1
      this.getRoleList()
    },
    add () {
      this.$refs.createModel.show('', '新增')
    },
    updateRow (row) {
      this.$refs.createModel.show(row, '修改')
    }
  }
}
</script>
